﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 接箍质检数据录 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int bid = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["BID"] != null)
                {
                    int.TryParse(Request.QueryString["BID"], out bid);
                }
                if (bid > 0)
                {
                    this.BID.Text = bid.ToString();
                    this.Button_Add.Enabled = true;
                    this.Button_JinDu.Enabled = true;
                    this.Button_JinDu.OnClientClick = "parent.swin2_load('/Fc/JinDu3_In.aspx?id=" + bid + "',640,480);";
                    this.SqlDataSource3.SelectParameters["BID"].DefaultValue = bid.ToString();
                    this.SqlDataSource3.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    this.Button_Add.Enabled = false;
                }
            }
            this.SqlDataSource1.SelectParameters["BID"].DefaultValue = this.BID.Text;

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共有 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            int zs;
            if (!int.TryParse(this.ZhiShu.Text, out zs))
            {
                this.Notice = "“支数”录入有误";
                return;
            }
            else if (zs <= 0)
            {
                this.Notice = "“支数”必须大于 0";
                return;
            }

            if (this.SqlDataSource1.Insert() > 0)
            {
                this.Notice = "增加成功";
            }

        }

        protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1]))
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[1].Value).ToLongDateString();
                this.ChanPin.Text = ((int)e.Command.Parameters[2].Value).ToString();
                this.BanCi.Text = ((int)e.Command.Parameters[3].Value).ToString();

                if ((int)e.Command.Parameters[3].Value > 0)
                {
                    ListItem li=new ListItem();
                    li.Text="车丝="+(int)e.Command.Parameters[4].Value;
                    li.Value="-1";
                    this.CheSiJi.Items.Add(li);
                    this.ZhiShu.Text = ((int)e.Command.Parameters[4].Value).ToString();

                    ListItem li2 = new ListItem();
                    li2.Text = "探伤=" + (int)e.Command.Parameters[8].Value;
                    li2.Value = "-2";
                    this.CheSiJi.Items.Add(li2);

                    ListItem li3 = new ListItem();
                    li3.Text = "探伤废=" + (int)e.Command.Parameters[9].Value;
                    li3.Value = "-3";
                    this.CheSiJi.Items.Add(li3);
                }


                string[] tmp=((string)e.Command.Parameters[5].Value).Split(new char[]{' '});
                for (int i = 0; i < tmp.Length; i++)
                {
                    if (tmp[i] != "0")
                    {
                        ListItem li = new ListItem();
                        li.Text = (i + 1) + "#车废=" + tmp[i];
                        li.Value = (i + 1).ToString();
                        this.CheSiJi.Items.Add(li);
                    }
                }
                if (!Convert.IsDBNull(e.Command.Parameters[6]))
                {
                    try
                    {
                        this.GongHuo.Text = ((int)e.Command.Parameters[6].Value).ToString();
                        this.LuHao.Text = (string)e.Command.Parameters[7].Value;
                    }
                    catch (Exception ex)
                    {
                        this.Notice = ex.Message;
                        this.Notice = "";
                    }
                }
            }
        }

        protected void CheSiJi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse( this.CheSiJi.Text) < 0)
            {
                this.YuanYin.Enabled = false;
                this.YuanYin.SelectedIndex = 0;
            }
            else
            {
                this.YuanYin.Enabled = true;
                this.YuanYin.SelectedIndex = 4;
            }
            this.ZhiShu.Text = this.CheSiJi.Items[this.CheSiJi.SelectedIndex].Text.Split(new char[] { '=' })[1];
        }
    }
}
